/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;

import cn.xphsc.kubernetes.core.query.NamespaceQuery;
import cn.xphsc.kubernetes.core.query.NetworkQuery;
import cn.xphsc.kubernetes.core.query.StorageQuery;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.NetworkingV1Api;
import io.kubernetes.client.openapi.apis.StorageV1Api;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: 用于获取Kubernetes中的所有network api Executor
 * @since 1.0.0
 */
public class NetworkApiExecutor<T> extends AbstractExecutor<T> {
    private String networkTypeName;
    private String networkName;
    private  T entity;
    private NetworkingV1Api networkingApi;
    private NetworkQuery networkQuery;
    private NamespaceQuery namespaceQuery;
    public NetworkApiExecutor(ApiClient client, NetworkQuery networkQuery, NetworkTypeMode networkTypeMode, NetworkMode networkMode) {
        super(client);
        this.networkTypeName=networkTypeMode.name();
        this.networkName=networkMode.name();
        networkingApi=new NetworkingV1Api(this.getClient());
        this.networkQuery=networkQuery;
    }
    public NetworkApiExecutor(ApiClient client, NamespaceQuery namespaceQuery, NetworkTypeMode networkTypeMode, NetworkMode networkMode) {
        super(client);
        this.networkTypeName=networkTypeMode.name();
        this.networkName=networkMode.name();
        networkingApi=new NetworkingV1Api(this.getClient());
        this.namespaceQuery=namespaceQuery;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(networkTypeName)&&StringUtils.isNotBlank(networkName)) {
            switch (networkTypeName){
                case "CREATE":
                    break;
                case "LIST":
                    switch (networkName){
                        case "DF":
                            try {
                                entity= (T) networkingApi.listIngressClass(networkQuery.pretty(), networkQuery.allowWatchBookmarks(), networkQuery._continue(), networkQuery.fieldSelector(), networkQuery.labelSelector(), networkQuery.limit(), networkQuery.resourceVersion(), networkQuery.resourceVersionMatch(), networkQuery.timeoutSeconds(), networkQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case "NS":
                            try {
                                entity= (T) networkingApi.listNamespacedIngress(namespaceQuery.namespace(),namespaceQuery.pretty(),namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case "ALLNS":
                            try {
                                entity= (T) networkingApi.listIngressForAllNamespaces(namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(),namespaceQuery.fieldSelector(),namespaceQuery.labelSelector(),namespaceQuery.limit(),namespaceQuery.pretty(),namespaceQuery.resourceVersion(),namespaceQuery.resourceVersionMatch(),namespaceQuery.timeoutSeconds(),namespaceQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        default:
                    }
                    break;

                case "PATCH":
                    break;
                case "READ":
                    break;
                case "REPLACE":
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum NetworkTypeMode {
        /**
         *
         */
        CREATE,
        LIST,
        PATCH,
        READ;
        private NetworkTypeMode() {

        }
        private NetworkTypeMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static enum NetworkMode {

        /**
         *
         */
        DF,

        NS,
        ALLNS,
        AUTO;

        private NetworkMode() {

        }
        private NetworkMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


